<?php

/*
 * 我的优惠券
 */

namespace Home\Controller;

class MycouponController extends BaseMemberController {

    function _initialize() {
        parent::_initialize();
        $this->_user_id = session('visitor.user_id');
        $this->_store_mod = M('store');
        $this->_coupon_mod = M('coupon');
        $this->_coupon_sn_mod = M('coupon_sn');
    }

    /**
     * @api {get} /index.php?m=Home&c=Mycoupon&a=get_coupon_list 获取我领取的优惠券列表
     * @apiName 获取优惠券详情 
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 接口详细描述 
     * 
     * @apiSuccess {String} coupon_sn 优惠券序列号
     * @apiSuccess {String} coupon_name 优惠券名称
     * @apiSuccess {String} store_id 优惠券店铺ID
     * @apiSuccess {String} coupon_value 优惠券金额
     * @apiSuccess {String} start_time 开始时间
     * @apiSuccess {String} end_time 结束时间
     * @apiSuccess {String} min_amount 最低使用金额
     * @apiSuccess {String} if_issue 是否发布
     * @apiSuccess {String} coupon_bg 优惠券背景图
     * @apiSuccess {String} mall_recommended 是否推荐
     * @apiSuccess {String} store_name 店铺名称
     * 
     */
    function get_coupon_list() {
        $map['user_id'] = $this->_user_id;

        $coupon_list = $this->_coupon_sn_mod
                ->field('coupon_sn.*,coupon.coupon_name,coupon.store_id,coupon.coupon_value,coupon.start_time,coupon.end_time,coupon.min_amount,coupon.if_issue,coupon.coupon_bg,coupon.mall_recommended,store.store_name')
                ->alias('coupon_sn')
                ->where($map)
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'coupon coupon ON coupon.coupon_id = coupon_sn.coupon_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store store ON store.store_id = coupon.store_id')
                ->page(I('request.page', 1, 'intval') . ',10')
                ->select();
        
        if($coupon_list){
            foreach ($coupon_list as $key => $coupon) {
                $coupon_list[$key]['start_time'] = date('Y-m-d', $coupon['start_time']);
                $coupon_list[$key]['end_time'] = date('Y-m-d', $coupon['end_time']);
            }
        }
        
        render('10000', '', array('coupon_list' => $coupon_list));
    }

    /**
     * @api {get} /index.php?m=Home&c=Mycoupon&a=get_coupon_detail 获取优惠券详情
     * @apiName 获取优惠券详情 
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取优惠券详情 
     * 
     * @apiParam {String} coupon_sn 优惠券序列号
     * 
     * @apiSuccess {String} coupon_sn 优惠券序列号
     * @apiSuccess {String} coupon_name 优惠券名称
     * @apiSuccess {String} store_id 优惠券店铺ID
     * @apiSuccess {String} coupon_value 优惠券金额
     * @apiSuccess {String} start_time 开始时间
     * @apiSuccess {String} end_time 结束时间
     * @apiSuccess {String} min_amount 最低使用金额
     * @apiSuccess {String} if_issue 是否发布
     * @apiSuccess {String} coupon_bg 优惠券背景图
     * @apiSuccess {String} mall_recommended 是否推荐
     * @apiSuccess {String} store_name 店铺名称
     */
    function get_coupon_detail() {
        $coupon_sn = I('request.coupon_sn');
        if (empty($coupon_sn)) {
            render('10200', '优惠券序列号为空');
        }
        $map['coupon_sn'] = $coupon_sn;
        $map['user_id'] = $this->_user_id;

        $coupon = $this->_coupon_sn_mod
                ->field('coupon_sn.*,coupon.coupon_name,coupon.store_id,coupon.coupon_value,coupon.start_time,coupon.end_time,coupon.min_amount,coupon.if_issue,coupon.coupon_bg,coupon.mall_recommended,store.store_name')
                ->alias('coupon_sn')
                ->where($map)
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'coupon coupon ON coupon.coupon_id = coupon_sn.coupon_id')
                ->join('LEFT JOIN ' . C('DB_PREFIX') . 'store store ON store.store_id = coupon.store_id')
                ->find();
        if (empty($coupon)) {
            render('10200', '优惠券序列号为空');
        }
        render('10000', '', array('coupon_list' => $coupon_list));
    }
    
    
    /**
     * @api {get} /index.php?m=Home&c=Mycoupon&a=add_coupon 添加优惠券
     * @apiName 获取店铺的所有优惠券
     * @apiGroup All
     * @apiVersion 0.1.0 
     * @apiDescription 获取店铺的所有优惠券
     * 
     * @apiParam {String} store_id 店铺ID
     * @apiParam {String} coupon_id 优惠券ID
     * 
     * @apiSuccess {String} coupon_id 优惠券ID
     * @apiSuccess {String} store_id 店铺ID
     * @apiSuccess {String} coupon_name 优惠券名称
     * @apiSuccess {String} coupon_value 优惠券金额
     * @apiSuccess {String} use_times 可使用次数
     * @apiSuccess {String} start_time 开始时间
     * @apiSuccess {String} end_time 结束时间
     * @apiSuccess {String} min_amount 满多少使用
     * @apiSuccess {String} if_issue 是否可用
     * @apiSuccess {String} coupon_bg 优惠券背景
     * @apiSuccess {String} mall_recommended 是否推荐
     */
    function add_coupon()
    {
        $coupon_id  = I('request.coupon_id',0,'intval');
        if(empty($coupon_id)){
            render('10201','优惠券ID为空');
        }
        $map_check['coupon_id'] = $coupon_id;
        $map_check['user_id'] = 0;
        //获取当前是否有足够领取的优惠券
        $coupon = $this->_coupon_sn_mod->where($map_check)->find();
        if (!$coupon) {
            render('10200', '优惠券已领完');
        }
        $map_check['user_id'] = $this->_user_id;
        //查看当前用户是否领取了优惠券
        $result = $this->_coupon_sn_mod->where($map_check)->find();
        if ($result) {
            render('10200', '您已领取了此优惠券');
        } else {
            $this->_coupon_sn_mod->where('coupon_sn='.$coupon['coupon_sn'])->save(array('user_id'=>  $this->_user_id));
            render('10000', '');
        }
    }
    
    

}

?>
